package com.starblink.search.search.ui.cell

import androidx.core.view.updateLayoutParams
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.ext.load
import com.starblink.basic.ext.screenWidth
import com.starblink.basic.route.RoutePage
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.search.databinding.CellRecommendStoreBinding

internal class RecommendStoreCell(
    binding: CellRecommendStoreBinding,
    private val subOrNotClick: ((model: MerchantWebVOF, position: Int) -> Unit)? = null
) :
    BaseBindingVH<MerchantWebVOF, CellRecommendStoreBinding>(binding) {

    init {
        itemView.updateLayoutParams {
            width = ((screenWidth - 32.dp2px()) / 4.0 + 0.5).toInt()
        }
    }

    override fun onBind(model: MerchantWebVOF, position: Int) {
        itemView.trackData(
            element = SpmElementDef.StoreIcon_33032,
            page = SpmPageDef.SearchRecommendBody,
            params = mutableMapOf(
                "merchantName" to (model.merchantName?:""),
                "index" to position.toString()
            ),
            businessSuffixID = model.id + itemView.context.hashCode(),
        )
        itemView.setOnClickListener {
            navigationTo(RoutePage.Store.PAGE_HOME) {
                extraOf(RoutePage.Store.ID to model.id)
            }
        }
        binding.tvName.text = model.merchantName


        binding.img.load(url = model.logo ?: "")
        binding.btnFollow.setImageResource(if (model.subOrNot) com.starblink.basic.style.R.mipmap.ic_search_store_follow else com.starblink.basic.style.R.mipmap.ic_search_store_unfollow)

        binding.btnFollow.trackData(
            element = SpmElementDef.HomeNewSubStoreSmallAdd,
            page = SpmPageDef.SearchRecommendBody,
            onlyHit = true,
            params = mutableMapOf(
                "merchantName" to (model.merchantName?:""),
                "index" to position.toString(),
                "result" to (if(model.subOrNot) "0" else "1" ),
                "status" to  (if(UserDataCenter.isLogin()) "1" else "0")
            )
        )

        binding.btnFollow.setOnClickListener {
            subOrNotClick?.invoke(model, position)
        }

    }
}